package top.wangjingxin.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

// @WebFilter(urlPatterns = "/api/v1/*")
public class AuthFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String url = httpServletRequest.getRequestURI();
        if (s(url) || httpServletRequest.getSession().getAttribute("id") != null) {
            chain.doFilter(request, response);
        } else {
            return;
        }
    }

    private boolean s(String url) {
        return !url.contains("add") && !url.contains("delete") && !url.contains("update")&&!url.contains("upload");
    }

    @Override
    public void destroy() {
    }
}
